<?php
declare (strict_types = 1);

namespace common\service;

class WechatService extends \think\Service
{

    /**
     * 获取token
     * @param $wechatConfig
     * @param $code
     * @return bool|\returns|string
     */
    public static function get_token($wechatConfig,$code)
    {
        if(is_array($wechatConfig)){
            $result = http_get($wechatConfig['get_token_url'],[
                'appid'=>$wechatConfig['app_id'],
                'secret'=>$wechatConfig['app_secret'],
                'code'=>$code,
                'grant_type'=>'authorization_code'
            ]);
            if(false == $result){
                return false;
            }
            $result = json_decode($result,true);
            return $result;
        }
    }

    /**
     * 获取用户信息
     * @param $wechatConfig
     * @param $access_token
     * @param $openid
     * @return bool|\returns|string
     */
    public static function get_user_info($wechatConfig,$access_token,$openid){
        $result = http_get($wechatConfig['get_user_info_url'],[
            'access_token'=>$access_token,
            'openid'=>$openid
        ]);
        if(false == $result){
            return false;
        }
        $result = json_decode($result,true);
        return $result;
    }
}
